#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "ql_oe.h"
#include "ql_quecfota_v2.h"

static int bat_level = 100;
static int rsrp = 0;
static nw_client_handle_type    h_nw     = 0;

#define BUF_SIZE 32

static inline char *input_fgets(char *msg, ...)
{
	static char ptr[128] = { 0 };
	va_list ap;

	va_start(ap, msg);
	vprintf(msg, ap);
	va_end(ap);
	memset(ptr, 0, sizeof(ptr));
	fgets(ptr, sizeof(ptr), stdin);
	if(strlen(ptr) > 0) {
		if('\n' == ptr[strlen(ptr) - 1]) {
			ptr[strlen(ptr) - 1] = '\0';
		}
	}
	return ptr;
}

static int test_nw(void)
{
    int    cmdIdx  = 0;
    int    ret     = E_QL_OK;
    char    buf[BUF_SIZE] = {0};    

	ret = QL_MCM_NW_Client_Init(&h_nw);
	printf("QL_MCM_NW_Client_Init ret = %d\n", ret);

  
	QL_MCM_NW_SIGNAL_STRENGTH_INFO_T    t_info = {0};

	memset(&t_info, 0, sizeof(QL_MCM_NW_SIGNAL_STRENGTH_INFO_T));
	ret = QL_MCM_NW_GetSignalStrength(h_nw, &t_info);
	printf("QL_MCM_NW_GetSignalStrength ret = %d, detail info:\n", ret);

	if(t_info.gsm_sig_info_valid)
	{
		printf("gsm_sig_info: rssi=%d\n", 
				t_info.gsm_sig_info.rssi);
	} 

	if(t_info.wcdma_sig_info_valid)
	{
		printf("wcdma_sig_info: rssi=%d, ecio=%d\n", 
				t_info.wcdma_sig_info.rssi, 
				t_info.wcdma_sig_info.ecio);
	} 
	if(t_info.tdscdma_sig_info_valid)
	{
		printf("tdscdma_sig_info: rssi=%d, rscp=%d, ecio=%d, sinr=%d\n", 
				t_info.tdscdma_sig_info.rssi, 
				t_info.tdscdma_sig_info.rscp,
				t_info.tdscdma_sig_info.ecio,
				t_info.tdscdma_sig_info.sinr);
	} 
	if(t_info.lte_sig_info_valid)
	{
		printf("lte_sig_info: rssi=%d, rsrq=%d, rsrp=%d, snr=%d\n", 
				t_info.lte_sig_info.rssi, 
				t_info.lte_sig_info.rsrq,
				t_info.lte_sig_info.rsrp,
				t_info.lte_sig_info.snr);
		rsrp = t_info.lte_sig_info.rsrp;
	} 
	if(t_info.cdma_sig_info_valid)
	{
		printf("cdma_sig_info: rssi=%d, ecio=%d\n", 
				t_info.cdma_sig_info.rssi, 
				t_info.cdma_sig_info.ecio);
	} 
	if(t_info.hdr_sig_info_valid)
	{
		printf("hdr_sig_info: rssi=%d, ecio=%d, sinr=%d, io=%d\n", 
				t_info.hdr_sig_info.rssi, 
				t_info.hdr_sig_info.ecio,
				t_info.hdr_sig_info.sinr,
				t_info.hdr_sig_info.io);
	} 
    return 0;
}

static void ql_fota_cb_func(S_QUEC_FOTA_RET_INFO info, void *contextPtr) {
	fprintf(stderr, "%s: quecfota state [%d] [%d]\n", __FUNCTION__, info.state, info.ret_code);
	if(info.info_len != 0)
		fprintf(stderr, "%s: quecfota info [%s]\n", __FUNCTION__, info.info);
}

int main(int argc, char* argv[])  
{
	char *sptr = NULL;
	int ret = RES_OK;
	test_nw();
	ql_add_quecfota_v2_callback(ql_fota_cb_func, NULL);
	while(1)
	{
		printf("\ncase 0: check quecfota_v2 update plan");
		printf("\ncase 1: do quecfota_v2 update");
		sptr = input_fgets("\nPlease input case: ");
		switch(atoi(sptr))
		{
			case 0:
			{
				ret = ql_start_quecfota_v2_check("http://47.116.137.111:8189",bat_level, rsrp);
				if (RES_OK == ret)
				{
					fprintf(stderr, "%s: ql_start_quecfota_v2_check  success\n", __FUNCTION__);
				}
				else
				{
					fprintf(stderr, "%s: ql_start_quecfota_v2_check  failed\n", __FUNCTION__);
				}
			}
			break;
			case 1:
			{
				ret = ql_start_quecfota_v2("http://47.116.137.111:8189",bat_level, rsrp);
				if (RES_OK == ret)
				{
					fprintf(stderr, "%s: quecfota start success\n", __FUNCTION__);
				}
				else
				{
					fprintf(stderr, "%s: quecfota start failed\n", __FUNCTION__);
				}
			}
			break;
			default:
				fprintf(stderr, "unknow case:%d",atoi(sptr));
			break;
		}
	}
	return 0;  
}
